
import SDKBase from "./SDKBase";
import { SDKState } from "../SDKDefine";
/**
 * 分包管理
 */
export default class SDKSubpackage extends SDKBase {

    /**
     * 
     * @param subname 分包的名字
     * @param callback 回调函数
     */
    loadSubpackage(subname: string, callback: (state: number, progress: number) => void) {
        if (!this.sdk.hasSubpackage()) {
            callback(0, 0)
            return;
        }
        const loadTask = this.sdk.loadSubpackage({
            name: subname, // name 可以填 name 或者 root
            success: (res: any) => {
                // 分包加载成功后通过 success 回调
                callback(SDKState.SUCCESS, 1)
            },
            fail: (res: any) => {
                // 分包加载失败通过 fail 回调
                callback(SDKState.FAIL, 1)
            },
        });

        loadTask.onProgressUpdate((res: { progress: number, }) => {
            callback(SDKState.LOADING, res.progress)
        })

    }
}
